package com.sicheng.lc.周赛.分类.设计;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/1 15:21
 */
public class 设计位集 {
    //https://leetcode.cn/problems/design-bitset/
    class Bitset {

        int size;
        int[] bits;
        int count = 0;
        boolean reversed;
        StringBuilder res = new StringBuilder((int) (1e5));

        public Bitset(int size) {
            this.size = size;
            bits = new int[size];
        }

        public void fix(int idx) {
            if (bits[idx] == 0) {
                if (!reversed) {
                    count++;
                    bits[idx] = 1;
                }
            } else {
                if (reversed) {
                    count++;
                    bits[idx] = 0;
                }
            }
        }

        public void unfix(int idx) {
            if (bits[idx] == 1) {
                if (!reversed) {
                    count--;
                    bits[idx] = 0;
                }
            } else {
                if (reversed) {
                    count--;
                    bits[idx] = 1;
                }
            }
        }

        public void flip() {
            count = size - count;
            reversed = !reversed;
        }

        public boolean all() {
            return count == size;
        }

        public boolean one() {
            return count > 0;
        }

        public int count() {
            return count;
        }

        public String toString() {

            res.delete(0, res.length());

            for (int bit : bits) {
                res.append(!reversed ? bit : 1 - bit);
            }
            return res.toString();
        }
    }

    public static void main(String[] args) {
        Bitset bitset = new 设计位集().new Bitset(5);
        bitset.fix(3);
        bitset.fix(1);
        bitset.flip();
        System.out.println(bitset.all());
        bitset.unfix(0);
        bitset.flip();
        System.out.println(bitset.one());
    }
}
